<?php
/**
 * 
 */

/**
 * 
 */
/**
 * Template Engine System
 * @author anthony
 */
class TemplateE extends Singleton {
    // Header and Footer which is <DOCTYPE.... in header_l0 right down to </html> in footer_l0 (Opening and closing basically)
    protected $header_level0 = false;
    protected $footer_level0 = false;
    
    // Between <head> and </head> and includes the other eclipse part
    protected $header_level1 = false;
    protected $footer_level1 = false;
    
    // Menu Links Go between these two
    protected $header_level2 = false;
    protected $footer_level2 = false;
    
    // Main Content Pane
    protected $header_level3 = false;
    protected $footer_level3 = false;
    
    protected $output = "";
    protected $nosidebar = false;
    protected $centralcontentexpand = false;
    protected $pageTitle = false;
    protected $pageFooter = false;
    protected $rendered = false;
    
    protected $menu_items_html = "";
    protected $main_html = "";
    protected $sidebar_html = "";
    protected $dropdown_username = "";
    protected $dropdown_html = "";
    
    protected $Templater = false;
    
    protected $script_add_pre = "";
    protected $script_add_post = "";
    
    
    /**
     * Constructor for this class
     * @global type $_ECLIPSEMDE
     * @throws RuntimeException 
     */
    protected function SingletonConstruct() {
	global $_ECLIPSEMDE;
	$tEngine = ___TEMPLATINGENGINE___;
	$this->Templater = new $tEngine();
	if(is_subclass_of($this->Templater, 'Template') === false) {
	    throw new RuntimeException("The Templater class named `".$tEngine."` is not a subclass of Template. Is this a Template Obejct?");
	}
	$templateDir = $_ECLIPSEMDE['ROOT_WD'].'core/template/templateFiles/';
	$this->header_level0 = $templateDir.'header_l0.html';
	$this->footer_level0 = $templateDir.'footer_l0.html';
	
	$this->header_level1 = $templateDir.'header_l1.html';
	$this->footer_level1 = $templateDir.'footer_l1.html';
	
	$this->header_level2 = $templateDir.'header_l2.html';
	$this->footer_level2 = $templateDir.'footer_l2.html';
	
	$this->header_level3 = $templateDir.'header_l3.html';
	$this->footer_level3 = $templateDir.'footer_l3.html';
	
	$this->header_level4 = $templateDir.'header_l4.html';
	$this->footer_level4 = $templateDir.'footer_l4.html';
    }
    
    /**
     * Destructor for template engine 
     */
    public function __destruct() {}
    
    /**
     * Render the HTML
     * @global type $PageTimer 
     */
    public function render() {
	if($this->rendered === true) {
	    trigger_error('Multiple Render Calls. Not Rendering again.');
	} else {
	    $this->rendered = true;
	    $this->appendhtml($this->file_contents($this->header_level0));
		    if($this->pageTitle === false) {
			//FIXME: Sitewide Default Title
			$this->appendhtml('<title>Eclipse MDE Portal</title>');
		    } else {
			$this->appendhtml($this->pageTitle);
		    }
	    $this->appendhtml($this->file_contents($this->footer_level0));
	    // Pre-pended code
		$this->appendhtml($this->script_add_pre);

	    $this->appendhtml($this->file_contents($this->header_level1));
	    $this->appendhtml($this->file_contents($this->footer_level1));
	    if(strlen($this->dropdown_username) !== 0) {
		$this->appendhtml($this->dropdown_username);
	    } else {
		$this->appendhtml('Guest');
	    }

	    $this->appendhtml($this->file_contents($this->header_level2));
		$this->appendhtml($this->dropdown_html);
	    $this->appendhtml($this->file_contents($this->footer_level2));
		$this->appendhtml($this->menu_items_html); // NavBar items
	    $this->appendhtml($this->file_contents($this->header_level3));


	    if($this->nosidebar === true) {
		$this->appendhtml(($this->centralcontentexpand === true ? '' : '<div class="span2"></div>'));
		$this->appendhtml('<div class="span'.($this->centralcontentexpand === true ? '12' : '8').'"><div class="row-fluid">');
	    } else {
		$this->appendhtml('<div class="span9">');
	    }
		$this->appendhtml($this->main_html); // Main HTML
	    if($this->nosidebar === true) {
		$this->appendhtml(($this->centralcontentexpand === true ? '</div></div>' : '</div></div><div class="span2"></div>'));
		//$this->appendhtml('</div></div><div class="span2"></div>');
	    } else {
		$this->appendhtml('</div>');
	    }
	    $this->appendhtml($this->file_contents($this->footer_level3));
	    if($this->nosidebar === false) {
		$this->appendhtml('<div class="span3"><div class="well sidebar-nav"><ul class="nav nav-list">');
		$this->appendhtml($this->sidebar_html); //Sidebar HTML
		$this->appendhtml('</div></div>');
	    }
	    $this->appendhtml($this->file_contents($this->header_level4));
		global $PageTimer;
		if(method_exists($PageTimer, 'finish')) {
		    $pRtime = 'Page Generated in '.$PageTimer->getTimeMilli().' ms.';
		} else {
		    $pRtime = '';
		}
		if(($this->pageFooter) !== false) {
		    $this->appendhtml($this->pageFooter.'&nbsp;|&nbsp;'.$pRtime);
		} else {
		    $this->appendhtml($pRtime);
		}

	    //appended code
		$this->appendhtml($this->script_add_post);
	    $this->appendhtml($this->file_contents($this->footer_level4));

		/*$this->appendhtml($this->file_contents($this->header_level4));
		$this->appendhtml($this->file_contents($this->footer_level4));*/



	    echo $this->output;
	    ob_flush();
	}
    }
    
    /**
     * Append HTML to the buffer.
     * @param string $html 
     */
    protected function appendhtml($html) {
	$this->output .= $html;
    }
    
    /**
     * Append File Contents to the buffer
     * @param type $filename
     * @return type 
     */
    protected function file_contents($filename) {
	return file_get_contents($filename);
    }
    
    /**
     * Return the templater object (such as bootstraptemplate)
     * @return Template 
     */
    public function getTemplater() {
	return $this->Templater;
    }
    
    /**
     * Set the page title
     * @param string $title 
     */
    public function setTitle($title) {
	//FIXME: Sitewide Default Title
	$this->pageTitle = '<title>Eclipse MDE :: '.$title.'</title>';
    }
    
    /**
     * Create a url "menu item".
     * @param string $uri
     * @param string $html 
     */
    public function create_menu_item($uri, $html) {
	$this->menu_items_html .= '<li><a href="'.$uri.'" target="_self">'.$html.'</a></li>';
    }
    
    /**
     * Add content to the main section of the site
     * @param string $html 
     */
    public function create_content_html($html) {
	$this->main_html .= $html;
    }
    
    /**
     * Add content to the sidebar section of the site
     * @param string $html 
     */
    public function create_sidebar_html($html) {
	$this->sidebar_html .= $html;
    }
    
    /**
     * Add content to the dropdown section of the site (usually where the users drop down settings etc is)
     * @param string $html 
     */
    public function create_dropdown_html($html) {
	$this->dropdown_html .= $html;
    }
    
    /**
     * Set the username on the user drop down menu
     * @param string $username 
     */
    public function set_dropdown_username($username) {
	$this->dropdown_username = $username;
    }
    
    /**
     * Set the footer for the site
     * @param string $footer 
     */
    public function set_footer($footer) {
	$this->pageFooter = $footer;
    }
    
    /**
     * Destroy any content in the output buffer
     */
    public function destroyContent() {
	if($this->rendered === true) {
	    trigger_error('DestroyContent will only destroy the TemplateEngine Output Buffer. The content has been rendered to the PHP ob and flushed (echo, ob_flush).');
	}
	$this->pageTitle = false;
	$this->menu_items_html = "";
	$this->main_html = "";
	$this->sidebar_html = "";
	/*$this->dropdown_html = "";
	$this->dropdown_username = "";*/
    }
    
    /**
     * Add a script call (such as javascript or CSS) to the start of the page
     * @param string $script 
     */
    public function add_preloadScript($script) {
	$this->script_add_pre .= $script;
    }
    
    /**
     * Add a script call (such as javascript) to the end of the page
     * @param type $script 
     */
    public function add_postloadScript($script) {
	$this->script_add_post .= $script;
    }
    
    public function setHasSidebar($boolean) {
	if(is_bool($boolean)) {
	    $this->nosidebar = ($boolean === true ? false : true);
	} else {
	    throw new RuntimeException("Boolean expected in ".__CLASS__."::".__METHOD__." but received `".$boolean."`.");
	}
    }
    
    public function setCentralContentFullWidth($boolean) {
	if(is_bool($boolean)) {
	    $this->centralcontentexpand = $boolean;
	} else {
	    throw new RuntimeException("Boolean expected in ".__CLASS__."::".__METHOD__." but received `".$boolean."`.");
	}
    }
}
$TemplateEngine = Singleton::CreateInstance('TemplateE');
?>
